package com.demo.controller;

import com.demo.exception.AjaxException;
import com.demo.tools.ResponseData;
import com.demo.bean.User;
import com.demo.service.IUserService;
import com.demo.tools.BaseResponse;
import com.demo.tools.JWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Api("user")
@Controller
public class UserController {
    //注入业务层
    @Autowired
    private IUserService userService;



    @RequestMapping(value = "/user",method = RequestMethod.POST) //mapping映射url路径
    @ResponseBody  //给客户端响应一段json数据也可以是字符串
    @ApiOperation(value="分页查询所有用户信息",httpMethod="POST",notes="备注",response=User.class)
    public BaseResponse index(Integer pageNo) {


        Integer pageSize = 2;
        PageHelper.startPage(pageNo, pageSize);
        List<User> users = userService.listAll();

        PageInfo<User> pageInfo = new PageInfo<User>(users);

        return BaseResponse.success(pageInfo);
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST) //mapping映射url路径
    @ResponseBody  //给客户端响应一段json数据也可以是字符串
    @ApiOperation(value="添加用户信息",httpMethod="POST",notes="备注",response=User.class)
    public BaseResponse addUser(User user) {
        String msg = userService.addUser(user);
        return BaseResponse.success(msg);
    }



    //处理登录
    @ApiOperation(value="登录获取token",httpMethod="POST",notes="备注",response=ResponseData.class)
    @RequestMapping(value="login", produces = "application/json; charset=utf-8",method = RequestMethod.POST)
    public @ResponseBody ResponseData login(HttpServletRequest request, @RequestParam( "name") String name,
                                            @RequestParam("password") String password) {
        User user = new User(name, password);

        ResponseData responseData = ResponseData.ok();
        //先到数据库验证
        User loginUser = userService.checkLogin(user);
        if(null != loginUser) {
            //给用户jwt加密生成token
            String token = JWT.sign(loginUser, 60L* 1000L* 30L);
            //封装成对象返回给客户端
            responseData.putDataValue("userId", loginUser.getId());
            responseData.putDataValue("token", token);
        }
        else{
            responseData =  ResponseData.customerError("登录失败");
        }
        return responseData;
    }


    //模拟抛出异常
    @RequestMapping(value = "/sx", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView sx(Integer type) {

        throw new AjaxException("Ajax调用出错");
    }

}
